#!/usr/bin/python

#Created by turk 2015-12-08
#Modified by turk 2016-09-30 Optimization logic
#Import Leak Data [PYTHON]
#Version 1.1 

import os
import commands
import sys
import logging
import shutil


WORKPATH='/root/work'
LOGPATH=WORKPATH + '/log'
LOGFILE=LOGPATH + '/leak_import.log'

#Logger
logging.basicConfig(level=logging.DEBUG,  
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',  
                    datefmt='%Y-%m-%d %H:%M:%S',  
                    filename=LOGFILE,  
                    filemode='w')  
console = logging.StreamHandler()  
console.setLevel(logging.INFO)  
# set print format
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')  
console.setFormatter(formatter)  
#
logging.getLogger('').addHandler(console)  
logger1 = logging.getLogger('myapp.area1')                     
                    

#ORACLE TNS
TNS_NAME='BSMPSB_GD'
USERNAME='sds'
PASSWORD='h16aug8v3w'


#export LD_LIBRARY_PATH=/home/ftpuser/instantclient_11_2
#export ORACLE_HOME=/home/ftpuser/instantclient_11_2
#export TNS_ADMIN=/home/ftpuser/instantclient_11_2
#export NLS_LANG=american_america.ZHS16GBK
#export PATH=$PATH:/home/ftpuser/instantclient_11_2
os.environ['LD_LIBRARY_PATH']=str("/home/instantclient_11_2")
os.environ['ORACLE_HOME']=str("/home/instantclient_11_2")
os.environ['TNS_ADMIN']=str("/home/instantclient_11_2")
os.environ['NLS_LANG']=str("american_america.ZHS16GBK")
os.environ['PATH']=str("$PATH:/home/instantclient_11_2")


SOURCEPATH='/data/hadoop/leak/total/'
DATAPATH='/data/hadoop/leak/import/'
CONTROLPATH=WORKPATH + '/control'


def sqlldrfunc(CTLFILE, DATAFILE, FILENAME):
	cmd='sqlldr '+USERNAME+'/'+PASSWORD+'@'+TNS_NAME+' control='+CONTROLPATH+'/'+CTLFILE+' data='+DATAFILE+' log='+LOGPATH+'/'+FILENAME+'.log bad='+LOGPATH+'/'+FILENAME+'.bad'
	logger1.info(cmd)
	RET=commands.getstatusoutput(cmd)
	logger1.info(RET)
    #if STATUS == 0:
    #    os.remove(DATAFILE)

def scanfile(FILENAME,CONTROLFILE):
	list = os.listdir(DATAPATH) #list all file dir
	for f in list:
		filepath = os.path.join(DATAPATH,f)
		if(os.path.isfile(DATAPATH + '/' + f) and FILENAME in f):
			sqlldrfunc(CONTROLFILE,filepath,f)
            #deletefile(filepath)
            

def delete():
    list = os.listdir(DATAPATH) #list all file dir
    for f in list:
        filepath = os.path.join(DATAPATH,f)
        if(os.path.isfile(DATAPATH + '/' + f)):
            os.remove(filepath)
            
def deletefile(DATAFILE):
    if(os.path.isfile(DATAFILE)):
        os.remove(FILE)            
            
if __name__ == '__main__':  
    cmd='/bin/mv ' + SOURCEPATH + '*.bak ' + DATAPATH
    logger1.info(cmd)
    RET=commands.getstatusoutput(cmd)
    logger1.info(RET)
    
    scanfile('siteInfo_','siteInfo.ctl')
    scanfile('serverInfo_','serverInfo.ctl')
    scanfile('serverRisk_','serverRisk.ctl')
    scanfile('vulInfo','vulInfo.ctl')
    scanfile('taskInfo','taskInfo.ctl')
    scanfile('riskInfo','riskInfo.ctl')
    delete()
    print 'COMPLATE'
